import BaseComponent from '@/components/BaseComponent';
import tool from '@/utils/tool';

// 监听屏幕变化，动态调整高度
export default class FillHeight extends BaseComponent {
  didMount() {
    this.bind(window, 'resize', this.update);
    this.update();
  }

  update = () => {
    if (!this.wrap) {
      return;
    }
    const { decrement = 0, style = {} } = this.props;
    this.style = Object.assign({}, style, {
      height: window.innerHeight - decrement - tool.getElementTop(this.wrap),
    });
    this.setState({});
  };

  render() {
    const { style, decrement, ...props } = this.props;
    return (
      <div
        ref={(n) => {
          this.wrap = n;
        }}
        style={this.style}
        {...props}
      />
    );
  }
}
